using System;
using System.Data;
using HRM.Common;
namespace HRM.BusinessLogic
{
    public class PositionInfo : Standard_Info
    {

        public PositionInfo()
        {
        }


        private Int32 _Position_Id;
        public Int32 Position_Id
        {
            get { return _Position_Id; }
            set { _Position_Id = value; }
        }

        private String _Position_Name;
        public String Position_Name
        {
            get { return _Position_Name; }
            set { _Position_Name = value; }
        }

        private String _Position_Branchcode;
        public String Position_Branchcode
        {
            get { return _Position_Branchcode; }
            set { _Position_Branchcode = value; }
        }
    }

    public class Position_Controller : Standard_Controller
    {
        #region "Contructor"
        private static Position_Controller _instance;
        protected Position_Controller() { }
        protected new void Dispose()
        {
            _instance = null;
        }
        private static object _syncLock = new object();
        public static Position_Controller Instance()
        {
            if (_instance == null)
            {
                lock (_syncLock)
                {
                    if (_instance == null)
                        _instance = new Position_Controller();
                }
            }
            return _instance;
        }
        public static Position_Controller Instance(String transName)
        {
            if (!String.IsNullOrEmpty(transName))
            {
                Position_Controller _instanceTrans = new Position_Controller();
                _instanceTrans.Trans_Name = transName;
                return _instanceTrans;
            }
            else
                return Instance();
        }
        #endregion

        public const String cs_Table_Name = "HRM_Position";

        public int Position_Using(int PositionID)
        {
            return Globals.Object_GetValue<int>(DataConnect.ExecuteScalar(DataConnect.WorkingConnect, Procedure_Name(cs_Table_Name, "HRM_Position_Using",false), PositionID));
        }

        public DataSet Position_GetAll()
        {
            return DataConnect.ExecuteDataset(DataConnect.WorkingConnect, Procedure_Name(cs_Table_Name, "HRM_Position_GetAll", false));
        }

        //public DataTable Position_GetAll()
        //{
        //    return DataConnect.ExecuteTable(DataConnect.WorkingConnect, Procedure_Name(cs_Table_Name, "HRM_Position_GetAll", false));
        //}

        public DataSet Position_GetList(out int ItemCount, string branchcode, int currentPage, int pageSize)
        {
            ItemCount = 0;
            DataSet ds = DataConnect.ExecuteDataset(DataConnect.WorkingConnect, Procedure_Name(cs_Table_Name, "HRM_Position_GetList", false), currentPage, pageSize,branchcode);
            if (ds != null && ds.Tables.Count > 0)
                ItemCount = Globals.Object_GetValue<int>(ds.Tables[1].Rows[0][0]);
            return ds;
        }

        public DataSet Position_GetByBranch(int BranchID)
        {
            return DataConnect.ExecuteDataset(DataConnect.WorkingConnect, Procedure_Name(cs_Table_Name, "HRM_Position_GetByBranchId",false), BranchID);
        }

        public DataSet Position_GetWithPosition(int PositionID)
        {
            return DataConnect.ExecuteDataset(DataConnect.WorkingConnect, Procedure_Name(cs_Table_Name, "HRM_Position_GetWithPosition", false), PositionID);
        }

        public PositionInfo Position_GetInfo(int PositionID)
        {
            return CBO.FillObject<PositionInfo>(DataConnect.ExecuteRow(DataConnect.WorkingConnect, Procedure_Name(cs_Table_Name, "HRM_Position_GetInfo", false), PositionID));
        }

        public int Position_Insert(PositionInfo eInfo)
        {
            return Globals.Object_GetValue<int>(DataConnect.ExecuteScalar(DataConnect.WorkingConnect, Procedure_Name(cs_Table_Name, "HRM_Position_Insert", false), eInfo.Position_Name, eInfo.Position_Branchcode));
        }

        public bool Position_Update(PositionInfo eInfo)
        {
            return Globals.Object_GetValue<bool>(DataConnect.ExecuteScalar(DataConnect.WorkingConnect, Procedure_Name(cs_Table_Name, "HRM_Position_Update", false), eInfo.Position_Id, eInfo.Position_Name, eInfo.Position_Branchcode));
        }

        public bool Position_Delete(int PositionID)
        {
            return Globals.Object_GetValue<bool>(DataConnect.ExecuteScalar(DataConnect.WorkingConnect, Procedure_Name(cs_Table_Name, "HRM_Position_Delete", false), PositionID));
        }


        public void InsertTable(DataTable dtData)
        {
            using (DataTable dt = dtData.GetChanges(DataRowState.Deleted))
            {
                if(dt!=null)
                    foreach (DataRow dr in dt.GetChanges(DataRowState.Deleted).Rows)
                    {
                        Position_Delete(Globals.DB_GetValue<int>(dr["Position_ID", DataRowVersion.Original]));
                    }
            }
            
            //
            dtData.AcceptChanges();
            //
            PositionInfo objInfo;
            foreach (DataRow dr in dtData.Rows)
            {
                objInfo = new PositionInfo();
                //
                Globals.DataRow_FillObject(dr, objInfo);
                if (objInfo.Position_Id > 0)
                    Position_Update(objInfo);
                else
                    Position_Insert(objInfo);
            }
        }

        public void Clear(string sBranch_Code)
        {
            
        }
    }
}
